﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Hangjing.Model;
using Hangjing.SQLServerDAL;
using Hangjing.Common;
using Hangjing.DBUtility;
using Hangjing.WebCommon;

/// <summary>
/// 客服一键发单  单页提交订单
/// </summary>
public partial class qy_54tss_Admin_Service_fastaddorder : System.Web.UI.Page
{
    Points daltogo = new Points();
    Hangjing.SQLServerDAL.ETogoShoppingCart dal = new Hangjing.SQLServerDAL.ETogoShoppingCart();

    protected IList<Hangjing.Model.ETogoShoppingCart> Foods
    {
        set
        {
            ViewState["Foods"] = value;
        }
        get
        {
            return ViewState["Foods"] == null ? null : (IList<Hangjing.Model.ETogoShoppingCart>)ViewState["Foods"];
        }
    }

    /// <summary>
    /// 经度
    /// </summary>
    protected string blng
    {
        set
        {
            ViewState["blng"] = value;
        }
        get
        {
            return ViewState["blng"] == null ? "" : ViewState["blng"].ToString();
        }
    }

    /// <summary>
    /// 纬度
    /// </summary>
    protected string blat
    {
        set
        {
            ViewState["blat"] = value;
        }
        get
        {
            return ViewState["blat"] == null ? "" : ViewState["blat"].ToString();
        }
    }


    /// <summary>
    /// 商家编号
    /// </summary>
    protected int  shopid
    {
        set
        {
            ViewState["shopid"] = value;
        }
        get
        {
            return ViewState["shopid"] == null ? 0 : Convert.ToInt32(ViewState["shopid"].ToString());
        }
    }


    /// <summary>
    /// 管理员
    /// </summary>
    protected string adminname
    {
        set
        {
            ViewState["adminname"] = value;
        }
        get
        {
            return ViewState["adminname"] == null ? "" : ViewState["adminname"].ToString();
        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
         

            string usercode = WebUtility.FixgetCookie("uc");

            EAdminInfo admin = UserHelp.GetAdmin();
            if (admin.CityID <= 0)
            {
                hfnotice.Value = "请登录城市管理员登录";
                return;
            }
            adminname = admin.AdminName;

            IList<PointsInfo> shops = new Points().GetList(1, 1, "cityid=" + admin.CityID + " and Star=2", "unid", 1);
            if (shops.Count < 0)
            {
                hfnotice.Value = "本城市没有设置本地商家，请联系管理员";
                return;
            }

            shopid = shops[0].Unid;


            hidLat.Value = "0";
            hidLng.Value = "0";

            hftid.Value = shops[0].Unid.ToString();
            hidTogoName.Value = shops[0].Name;
            tbCommission.Value = "0";

            OrderTime ot = new OrderTime(ddltime, shops[0]);

        }
    }

    protected void add_click(object sender, EventArgs e)
    {
        string usercode = WebUtility.FixgetCookie("uc");

        Custorder orderdal = new Custorder();
        CustorderInfo infos = new CustorderInfo();
        infos.OrderRcver = WebUtility.InputText(this.tbuname.Text);
        string maddress = WebUtility.InputText(this.tbaddress.Text);
        infos.AddressText = maddress;
        infos.OrderComm = WebUtility.InputText(this.tbtel.Text.Trim());
        infos.UserId = 0;
        infos.OrderAttach = WebUtility.InputText(this.tbremark.Value);

        infos.cityid = 0;
        infos.OrderAddress = adminname;
        infos.ReveInt1 = 1;
        infos.ReveInt2 = 0;
        infos.fromweb = ((int)OrderSource.CallCenter).ToString();
        infos.tempcode = usercode;
        infos.SendTime = Convert.ToDateTime(ddltime.SelectedValue);
        infos.tempcode = "";
        infos.CustomerName = "";
        infos.ReveVar1 = "0";

        string ulat = hidLat.Value;
        string ulng = hidLng.Value;

        infos.OrderSums = Convert.ToDecimal(tbPrice.Value);
        infos.shopdiscountmoney = Convert.ToDecimal(tbshopdiscountmoney.Value);
        infos.OldPrice = infos.OrderSums;
        infos.cityid = UserHelp.GetAdmin().CityID;
        infos.TogoId = shopid;
        infos.P2Sign = "";
        infos.paytime = Convert.ToDateTime("1970-1-1");
        infos.paymode = 4;
        infos.ReveDate1 = DateTime.Now;
        infos.cardpay = Convert.ToDecimal(tbcardpay.Value);
        infos.cardids = WebUtility.InputText(tbcardids.Value);

        infos.Commission = Convert.ToDecimal(tbCommission.Value);
        infos.settlecycle = 0;
        infos.Advance = WebUtility.InputText(this.tbAdvance.Value,'c');
        infos.Mergeorderremark = WebUtility.InputText(tbMergeorderremark.Value);
        infos.SystemUserId = Convert.ToInt32(ddlSystemUserId.SelectedValue);
        infos.IsShopSet = 1;
        infos.ReveDate1 = DateTime.Now;

        IList<Hangjing.Model.ETogoShoppingCart> list = Foods;

        infos.ReveVar2 = "{'ulat':'" + blat + "','ulng':'" + blng + "','slat':'" + 0 + "','slng':'" + 0 + "'}";


        try
        {
            if (orderdal.AddTBOrder(infos) > 0)
            {
                dal.DelAllCartItem(usercode);
                WebUtility.FixdelCookie("crm_tell");
                WebUtility.FixdelCookie("crm_uid");
                WebUtility.FixdelCookie("used_addressid");

        
                AlertScript.RegScript(this.Page, UpdatePanel1, "alert('订单提交成功!');top.location.href  = top.location.href;");
            }
            else
            {
                AlertScript.RegScript(this.Page, UpdatePanel1, "alert('订单失败!')");
            }
        }
        catch (Exception ex)
        {
            AlertScript.RegScript(this.Page, UpdatePanel1, "alert('订单失败,请联系管理员!')");
            Hangjing.AppLog.AppLog.Error("提交订单失败:管理员=" + UserHelp.GetAdmin() + "\r\n订单信息=" + Newtonsoft.Json.JsonConvert.SerializeObject(infos) + "\r\n错误信息=" + ex.ToString());

        }

       
    }

}
